//
//  JSPatternEditView.m
//  JigSaw
//
//  Created by slim on 15/2/1.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSPatternEditView.h"
#import "UIColor+Help.h"
#import "UIView+border.h"
//#import <ReactiveCocoa/ReactiveCocoa.h>
//#import <ReactiveCocoa/RACEXTScope.h>
@implementation JSPatternEditView

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.colorBtn0.btnColor = [UIColor colorWithHex:0xe6c9cc];
    self.colorBtn0.backgroundColor = [UIColor clearColor];
    self.colorBtn1.btnColor = [UIColor colorWithHex:0xf7c5b0];
    self.colorBtn1.backgroundColor = [UIColor clearColor];
    self.colorBtn2.btnColor = [UIColor colorWithHex:0xa9dac9];
    self.colorBtn2.backgroundColor = [UIColor clearColor];
    self.colorBtn3.btnColor = [UIColor colorWithHex:0xbee4e6];
    self.colorBtn3.backgroundColor = [UIColor clearColor];
    self.colorBtn4.btnColor = [UIColor colorWithHex:0xe6dbc5];
    self.colorBtn4.backgroundColor = [UIColor clearColor];

    self.colorBtn5.btnColor = [UIColor colorWithHex:0xffffff alpha:0];
    self.colorBtn5.backgroundColor = [UIColor clearColor];
    [self.colorBtn5 setImage:[UIImage imageNamed:@"color_btn5"] forState:UIControlStateNormal];
    self.selectedColorBtn = self.colorBtn5;
    self.colorBtn6.btnColor = [UIColor colorWithHex:0xb7afab];
    self.colorBtn6.backgroundColor = [UIColor clearColor];
    
    @weakify(self)
    self.colorBtn0.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.colorBtn0Blk([self getColorWithOutAlpha:self.colorBtn0.btnColor]);
        [self handleColorBtnClick:self.colorBtn0 buttonImage:[UIImage imageNamed:@"color_btn0"]];
        return [RACSignal empty];
    }];
    
    self.colorBtn1.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        
        self.colorBtn1Blk([self getColorWithOutAlpha:self.colorBtn1.btnColor]);
        [self handleColorBtnClick:self.colorBtn1 buttonImage:[UIImage imageNamed:@"color_btn1"]];
        return [RACSignal empty];
    }];
    
    self.colorBtn2.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.colorBtn2Blk([self getColorWithOutAlpha:self.colorBtn2.btnColor]);
        [self handleColorBtnClick:self.colorBtn2 buttonImage:[UIImage imageNamed:@"color_btn2"]];
        return [RACSignal empty];
    }];
    
    self.colorBtn3.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.colorBtn3Blk([self getColorWithOutAlpha:self.colorBtn3.btnColor]);
        [self handleColorBtnClick:self.colorBtn3 buttonImage:[UIImage imageNamed:@"color_btn3"]];
        return [RACSignal empty];
    }];
    
    self.colorBtn4.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.colorBtn4Blk([self getColorWithOutAlpha:self.colorBtn4.btnColor]);
        [self handleColorBtnClick:self.colorBtn4 buttonImage:[UIImage imageNamed:@"color_btn4"]];
        return [RACSignal empty];
    }];

    self.colorBtn5.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.colorBtn5Blk([self getColorWithOutAlpha:self.colorBtn5.btnColor]);
        [self handleColorBtnClick:self.colorBtn5 buttonImage:[UIImage imageNamed:@"color_btn5"]];
        return [RACSignal empty];
    }];
    
    self.colorBtn6.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.colorBtn6Blk([self getColorWithOutAlpha:self.colorBtn6.btnColor]);
        [self handleColorBtnClick:self.colorBtn6 buttonImage:[UIImage imageNamed:@"color_btn6"]];
        return [RACSignal empty];
    }];
    
    self.patternBtn0.backgroundColor = [UIColor colorWithHex:0xffffff];
    [self.patternBtn0 addBorder];
    self.patternBtn0.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.patternBtn0Blk();
        [self resetAllPattenBtn];
        [self.patternBtn0 addBorder];
        return [RACSignal empty];
    }];
    self.patternBtn1.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.patternBtn1Blk();
        [self resetAllPattenBtn];
        [self.patternBtn1 addBorder];
        return [RACSignal empty];
    }];
    self.patternBtn2.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.patternBtn2Blk();
        [self resetAllPattenBtn];
        [self.patternBtn2 addBorder];
        return [RACSignal empty];
    }];
    self.patternBtn3.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.patternBtn3Blk();
        [self resetAllPattenBtn];
        [self.patternBtn3 addBorder];
        return [RACSignal empty];
    }];
    self.patternBtn4.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.patternBtn4Blk();
        [self resetAllPattenBtn];
        [self.patternBtn4 addBorder];
        return [RACSignal empty];
    }];
    self.patternBtn5.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        self.patternBtn5Blk();
        [self resetAllPattenBtn];
        [self.patternBtn5 addBorder];
        return [RACSignal empty];
    }];

}

- (UIColor*)getColorWithOutAlpha:(UIColor*)color
{
    CGFloat red;
    CGFloat green;
    CGFloat blue;
    [color getRed:&red green:&green blue:&blue alpha:nil];
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

- (void)handleColorBtnClick:(JSCircleButton*)colorBtn buttonImage:(UIImage*)image
{
    self.selectedColorBtn.btnColor = [UIColor setColorAlpha:self.selectedColorBtn.btnColor withAlpha:1];
    [self.selectedColorBtn setNeedsDisplay];
    [self.selectedColorBtn setImage:nil forState:UIControlStateNormal];
    colorBtn.btnColor = [UIColor setColorAlpha:colorBtn.btnColor withAlpha:0];
    self.selectedColorBtn = colorBtn;
    [self.selectedColorBtn setImage:image forState:UIControlStateNormal];
    [self.selectedColorBtn setNeedsDisplay];
}


- (void)resetAllPattenBtn
{
    [self.patternBtn0 removeBorder];
    [self.patternBtn1 removeBorder];
    [self.patternBtn2 removeBorder];
    [self.patternBtn3 removeBorder];
    [self.patternBtn4 removeBorder];
    [self.patternBtn5 removeBorder];
}

- (void)resetColorButtons
{
    [self handleColorBtnClick:self.colorBtn5 buttonImage:[UIImage imageNamed:@"color_btn5"]];
}
@end
